﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chowpoo.Common;
using Chowpoo.Util;
using CourseSystem.Common;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using CourseSystem.Core.Enum;
using Webdiyer.WebControls.Mvc;

namespace CourseSystem.Web.Controllers
{
    [UserAuth(UserType = UserTypeEnum.Guess)]
    public class AdminGuideController : BaseController
    {
        //
        // GET: /AdminGuide/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult MenuListUc(int? pid)
        {
            CommonRequest.ClearCache();
            var list = CourseGuideMenuBLL.GetList();
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

        public ActionResult MenuSub(int id)
        {
            var info = CourseGuideMenuBLL.Get(id);
            if (info == null)
                return View("NotFound");
            return View(info);
        }

        public ActionResult MenuSubEdit(int? id, int? cid, string ran)
        {
            ViewData["ranStr"] = ran;
            if (string.IsNullOrEmpty(ran))
            {
                var ranStr = RandomStr.GetByTime(UserHelper.LoginUser.Id + "_" + (byte)UserHelper.LoginUser.UserType + "_");
                ViewData["ranStr"] = ranStr;
                if (string.IsNullOrEmpty(ranStr))
                    return View("NotFound");
            }
            else
            {
                if (!RandomStr.Check(ran))
                    return View("NotFound");
            }
            if (id.HasValue)
            {
                var info = CourseGuideSubMenuBLL.Get(id.Value);
                if (info == null)
                    return View("NotFound");
                return View(info);
            }
            if (cid.HasValue)
                return View(new CourseGuideSubMenuInfo { MenuId = cid.Value });
            return View("NotFound");
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult MenuSubEdit(CourseGuideSubMenuInfo info, string ran, int cid)
        {
            ViewData["ranStr"] = ran;
            if (!RandomStr.Check(ran))
                return View("NotFound");
            ViewData.Model = info;
            if (!ModelState.IsValid)
            {
                return View();
            }
            if (info.Id != 0)
            {
                var oldInfo = CourseGuideSubMenuBLL.Get(info.Id);
                if (oldInfo == null)
                    return View("NotFound");
                oldInfo.Name = info.Name;
                oldInfo.CommonContentInfo.DetailContent = (info.CommonContentInfo.DetailContent ?? "");
                if (CommonContentBLL.Edit(oldInfo.CommonContentInfo) && CourseGuideSubMenuBLL.Edit(oldInfo))
                {
                    //RandomStr.Remove(ran);
                    AttachmentsHelper.ClearUselessAttach(oldInfo.CommonContentInfo.DetailContent, ran, oldInfo.ContentId);
                    ViewData["AlertMsg"] = "编辑成功.";
                    return View(oldInfo);
                }
                ViewData["AlertMsg"] = "未知错误.";
                return View();
            }
            if (cid == 0)
            {
                ViewData["AlertMsg"] = "参数丢失,请重新添加.";
                return View();
            }
            info.CommonContentInfo.DetailContent = info.CommonContentInfo.DetailContent ?? "";
            info.MenuId = cid;
            if (CourseGuideSubMenuBLL.Add(info))
            {
                // RandomStr.Remove(ran);
                AttachmentsHelper.ClearUselessAttach(info.CommonContentInfo.DetailContent, ran, info.ContentId);
                TempData["AlertMsg"] = "添加成功.";
                return RedirectToAction("MenuSubEdit", new { id = info.Id, ran = ran });
            }
            ViewData["AlertMsg"] = "未知错误.";
            return View();
        }

        public ActionResult MenuSubDel(int id)
        {
            var info = CourseGuideSubMenuBLL.Get(id);
            if(info!=null)
            {
                var toDelAttachs = info.CommonContentInfo.AttachmentsInfo.ToList();
                if(CourseGuideSubMenuBLL.Del(id))
                {
                    AttachmentsBLL.DelFileByList(toDelAttachs);
                    SetAlertMsg("删除成功.", true);
                }
                return RedirectToAction("MenuSub", new { id = info.MenuId });
            }
            return NotFound();
        }

        public ActionResult MenuSubListUc(int id, int? pid)
        {
            CommonRequest.ClearCache();
            var list = CourseGuideSubMenuBLL.GetList(id).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

        public ActionResult Notice()
        {
            return View();
        }

        public ActionResult NoticeListUc(int? pid, string search, string startDate, string endDate)
        {
            CommonRequest.ClearCache();
            var list = CourseNoticeBLL.GetList(search, startDate.CheckNoDatetime(), endDate.CheckNoDatetime()).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

        public ActionResult NoticeEdit(int? id)
        {
            if (id.HasValue)
            {
                var info = CourseNoticeBLL.Get(id.Value);
                if (info == null)
                    return View("NotFound");
                return PartialView(info);
            }
            return PartialView(new CourseNoticeInfo { NoticeContent = "" });
        }

        [HttpPost]
        public ActionResult NoticeEdit(CourseNoticeInfo info)
        {
            if (!ModelState.IsValid)
                return PartialView(info);
            if (info.Id != 0)
            {
                var oldInfo = CourseNoticeBLL.Get(info.Id);
                if (oldInfo == null)
                    return View("NotFound");
                oldInfo.NoticeContent = info.NoticeContent ?? "";
                if (CourseNoticeBLL.Edit(oldInfo))
                {
                    ViewData["msg"] = "编辑成功.";
                    return PartialView(oldInfo);
                }
                ViewData["msg"] = "未知错误.";
                return PartialView(info);
            }

            info.NoticeTime = DateTime.Now;
            if (CourseNoticeBLL.Add(info))
            {
                ViewData["msg"] = "编辑成功.";
                return PartialView(info);
            }
            ViewData["msg"] = "未知错误.";
            return PartialView(info);
        }

        public ActionResult NoticeDel(int id)
        {
            if (CourseNoticeBLL.Del(id))
            {
                TempData["AlertMsg"] = "删除成功.";
                return RedirectToAction("Notice");
            }
            TempData["AlertMsg"] = "未知错误.";
            return RedirectToAction("Notice");
        }
    }
}
